<?php
require_once 'plugins/sfPropelPlugin/lib/propel/builder/SfPeerBuilder.php';
class OfPropelPeerBuilder extends SfPeerBuilder
{

	protected function addSelectMethods(&$script)
	{
		parent::addSelectMethods($script);
		self::addDoSelectAssoc($script);
		self::addAddJoin($script);
		
		$includeJoinAll = true;
		$table = $this->getTable();

		foreach ($this->getTable()->getForeignKeys() as $fk) {
			$tblFK = $table->getDatabase()->getTable($fk->getForeignTableName());
			if ($tblFK->isForReferenceOnly()) {
				$includeJoinAll = false;
			}
		}

		if ($includeJoinAll) {
			$this->addAddJoinAll($script);
		}

	}

	/**
	 * join all
	 *
	 * @param string $script
	 */
	protected function addAddJoinAll(&$script)
	{
		$table = $this->getTable();
		$className = $table->getPhpName();

		$script .= "
  /**
   * Join Related Tables
   * 
   * @param Criteria \$c
   */
  public static function addJoinAll(Criteria \$c)
  {
    ";

		foreach ($table->getForeignKeys() as $fk) {
			// want to cover this case, but the code is not there yet.
			if ( $fk->getForeignTableName() != $table->getName() ) {
				$joinTable = $table->getDatabase()->getTable($fk->getForeignTableName());
				$joinedTablePeerBuilder = OMBuilder::getNewPeerBuilder($joinTable);

				$joinClassName = $joinTable->getPhpName();
				$lfMap = $fk->getLocalForeignMapping();
				foreach ($fk->getLocalColumns() as $columnName ) {
					$column = $table->getColumn($columnName);
					$columnFk = $joinTable->getColumn( $lfMap[$columnName]);
					$script .= "
    \$c->addJoin(".$this->getColumnConstant($column).", ".$joinedTablePeerBuilder->getColumnConstant($columnFk).");
    ".$joinedTablePeerBuilder->getPeerClassname()."::addSelectColumns(\$c);
";
				}
			} // if fk->getForeignTableName != table->getName
		} // foreach [sub] foreign keys

		$script.="
   }
";


	}



	/**
	 * select method by assoc
	 *
	 * @param string $script
	 */
	protected function addDoSelectAssoc(&$script)
	{

		$script .= "
			/**
			 * select ResultSet::FETCHMODE_ASSOC
			 * 
			 * @param \$c Criteria
			 * @param \$con Connection
			 * 
			 * @return PDOStatement
			 */
			public static function doSelectAssoc(Criteria \$c, \$con=null)
			{
			  self::addSelectColumns(\$c);
			  return OfPropelPeer::doSelectAssoc(\$c, \$con);
			}
			 
		";

	}



	protected function addAddJoin(&$script)
	{

		$table = $this->getTable();
		$className = $table->getPhpName();
		$countFK = count($table->getForeignKeys());

		if ($countFK >= 1) {

			foreach ($table->getForeignKeys() as $fk) {

				$joinTable = $table->getDatabase()->getTable($fk->getForeignTableName());

				if (!$joinTable->isForReferenceOnly()) {

					if ( $fk->getForeignTableName() != $table->getName() ) {

						$joinClassName = $joinTable->getPhpName();

						$thisTableObjectBuilder = OMBuilder::getNewObjectBuilder($table);
						$joinedTableObjectBuilder = OMBuilder::getNewObjectBuilder($joinTable);
						$joinedTablePeerBuilder = OMBuilder::getNewPeerBuilder($joinTable);

						$script .= "

  /**
   * joinning ".$thisTableObjectBuilder->getFKPhpNameAffix($fk, $plural = false)." table
   *
   * @param Criteria \$criteria
   * 
   */
  public static function addJoin".$thisTableObjectBuilder->getFKPhpNameAffix($fk, $plural = false)."(Criteria \$criteria)
  {
";
						$lfMap = $fk->getLocalForeignMapping();
						foreach ($fk->getLocalColumns() as $columnName ) {
							$column = $table->getColumn($columnName);
							$columnFk = $joinTable->getColumn( $lfMap[$columnName] );
							$script .= "
    \$criteria->addJoin(".$this->getColumnConstant($column).", ".$joinedTablePeerBuilder->getColumnConstant($columnFk).");
    ".$joinedTablePeerBuilder->getPeerClassname()."::addSelectColumns(\$criteria);
";
						}

						$script .= "
  }
";
					} // if fk table name != this table name
				} // if ! is reference only
			} // foreach column
		} // if count(fk) > 1

	}

}